(function () {
    define(["angular"], function (angular) {
        var DataTreeHelperProto = DataTreeHelper.prototype;

        function DataTreeHelper() {
        }

        DataTreeHelperProto._cancelOtherEditable = function (treeData, item) {
            var i, len, el;
            if (!treeData || treeData.length === 0) return;
            for (i = 0, len = treeData.length; i < len; i++) {
                el = treeData[i];
                if (el.idProp !== item.idProp) {
                    el.editable = false;
                }
                if (el.children && el.children.length > 0) {
                    arguments.callee(el.children, item);
                }
            }
        };

        DataTreeHelperProto.removeItem = function (source, item) {
            var i, len = source.length;
            for (i = 0; i < len; i++) {
                if (source[i] === item) {
                    source.splice(i, 1);
                    break;
                }
            }
        };

        DataTreeHelperProto.remove = function (source, item) {
            for (var i = 0; i < source.length; i++) {
                if (source[i].idProp === item.idProp) {
                    source.splice(i, 1);
                    break;
                }
            }
        };

        DataTreeHelperProto.contains = function (source, item) {
            var i, len, ele,
                flag = false;
            for (i = 0, len = source.length; i < len; i++) {
                ele = source[i];
                if (ele.idProp === item.idProp) {
                    flag = true;
                    break;
                }
            }
            return flag;
        };

        //根据id查找结点
        DataTreeHelperProto.searchNodeByTd = function (treeData, id) {
            var i, len, el, result, childRes;
            for (i = 0, len = treeData.length; i < len; i++) {
                el = treeData[i];
                if (el.idProp === id) {
                    result = el;
                    break;
                }
                if (el.children && el.children.length > 0) {
                    childRes = arguments.callee(el.children, id);
                }
                if (childRes) {
                    result = childRes;
                    break;
                }
            }
            return result;
        };

        //根据值查找结点
        DataTreeHelperProto.findNodeByValue = function (treeData, value) {
            var result = [], childItems = [],
                i, len, el;
            for (i = 0, len = treeData.length; i < len; i++) {
                el = treeData[i];
                if (el.valueProp === value) {
                    result.push(el);
                }
                if (el.children && el.children.length > 0) {
                    childItems = arguments.callee(el.children, value);
                }
                if (childItems && childItems.length > 0) {
                    result = result.concat(childItems);
                    childItems = [];
                }
            }
            return result;
        };

        //根据显示值查找结点
        DataTreeHelperProto.findNodeByDisplay = function (treeData, value) {
            var result = [], childItems = [],
                i, len, el;
            for (i = 0, len = treeData.length; i < len; i++) {
                el = treeData[i];
                if (el.displayExpress.indexOf(value) !== -1) {
                    result.push(el);
                }
                if (el.children && el.children.length > 0) {
                    childItems = arguments.callee(el.children, value);
                }
                if (childItems && childItems.length > 0) {
                    result = result.concat(childItems);
                    childItems = [];
                }
            }
            return result;
        };
        //判断结点是否可勾选
        DataTreeHelperProto.canCheck = function(item,config){
            var result = false;
            if (item.showBox) {
                if (config.mode==="radio") {
                    if (item.type === 'leaf') {
                        result = true;
                    }
                } else {
                    if (item.type === 'leaf') {
                        result = true;
                    } else if (config.allowChooseParent==='true') {
                        result = true;
                    }
                }
            }
            return result;
        };
        //生成结点
        DataTreeHelperProto.createNode = function (item, config) {
            var prop = config.idProp,
                parentProp = config.parentProp,
                hasChildProp = config.hasChildProp,
                valueProp = config.valueProp,
                displayExpress = config.displayExpress,
                showBox = config.showBox,
                isExpandAll = config.isExpandAll,
                async = config.async,
                displayItem = config.displayItem,
                editable = config.editable,
                readonlyItem = config.readonlyItem,
                disabledItem = config.disabledItem,
                checkedProp = config.checkedProp,
                ele, el, show, display, editable1, readonly, disabled;
            el = item;
            ele = {};
            ele.idProp = el[prop];
            ele.parentProp = el[parentProp];
            ele.valueProp = el[valueProp];
            ele.displayExpress = el[displayExpress];
            ele.showBox = true;
            if (angular.isFunction(showBox())) {
                ele.showBox = (showBox()(el) === true);
            } else {
                show = showBox(el);
                if (show !== undefined) {
                    ele.showBox = (show === true);
                }
            }
            ele.originData = el;
            ele.children = [];
            ele.display = true;
            if (angular.isFunction(displayItem())) {
                ele.display = (displayItem()(el) === true);
            } else {
                display = displayItem(el);
                if (display !== undefined) {
                    ele.display = (display === true);
                }
            }
            ele.editable = false;
            if (angular.isFunction(editable())) {
                ele.editable = (editable()(el) === true);
            } else {
                editable1 = editable(el);
                if (editable1 !== undefined) {
                    ele.editable = (editable1 === true);
                }
            }
            ele.readonly = false;
            if (angular.isFunction(readonlyItem())) {
                ele.readonly = (readonlyItem()(el) === true);
            } else {
                readonly = readonlyItem(el);
                if (readonly !== undefined) {
                    ele.readonly = (readonly === true);
                }
            }
            ele.disabled = false;
            if (angular.isFunction(disabledItem())) {
                ele.disabled = (disabledItem()(el) === true);
            } else {
                disabled = disabledItem(el);
                if (disabled !== undefined) {
                    ele.disabled = (disabled === true);
                }
            }
            ele.expanded = false;
            if (async === "true") {
                if (el[hasChildProp] === 'true' || el[hasChildProp] === true || !hasChildProp) {
                    ele.type = "branch";
                } else {
                    ele.type = "leaf";
                }
            } else {
                ele.type = "leaf";
                if (isExpandAll === "true") {
                    ele.expanded = true;
                }
            }
            ele.parent = null;
            ele.selected = false;
            ele.checked = (el[checkedProp] === "true" || el[checkedProp] === true) && this.canCheck(ele,config);
            return ele;
        };

        //构建树
        DataTreeHelperProto.buildTree = function (source, config) {
            var me = this,
                result = [],
                tempArray = [],
                tempObject = {},
                i, el, ele;
            if (!source || source.length === 0) {
                return result;
            }
            for (i = 0; i < source.length; i++) {
                el = source[i];
                ele = me.createNode(el, config);
                tempArray.push(ele);
                tempObject[ele.idProp] = ele;
            }
            for (i = 0; i < tempArray.length; i++) {
                el = tempArray[i];
                if (el.parentProp) {
                    tempObject[el.parentProp].children.push(el);
                    tempObject[el.parentProp].type = "branch";
                    el.parent = tempObject[el.parentProp];
                } else {
                    result.push(el);
                }
            }
            return result;
        };

        //创建子孙结点
        DataTreeHelperProto.createChildren = function (item, records, config, isChildren) {
            var me = this,
                result = [],
                tempArray = [],
                tempObject = {},
                i, len, el, ele;
            if (!records || records.length === 0) {
                return result;
            }
            for (i = 0, len = records.length; i < len; i++) {
                el = records[i];
                ele = me.createNode(el, config);
                if (isChildren) {
                    ele.expanded = true;
                }
                tempArray.push(ele);
                tempObject[ele.idProp] = ele;
            }
            for (i = 0, len = tempArray.length; i < len; i++) {
                el = tempArray[i];
                if (el.parentProp) {
                    if(tempObject[el.parentProp]){
                        tempObject[el.parentProp].children.push(el);
                        tempObject[el.parentProp].type = "branch";
                        el.parent = tempObject[el.parentProp];
                    }
                    if (el.parentProp === item.idProp) {
                        result.push(el);
                    }
                }
            }
            return result;
        };

        //加载子结点
        DataTreeHelperProto.transChildren = function (records, config) {
            var me = this,
                result = [],
                i, len, el, ele;
            for (i = 0, len = records.length; i < len; i++) {
                el = records[i];
                ele = me.createNode(el, config);
                result.push(ele);
            }
            return result;
        };

        //初始化所有已选中结点
        DataTreeHelperProto.pushCheckedItem = function (treeData, scope) {
            var i, len, ele, child;
            for (i = 0, len = treeData.length; i < len; i++) {
                ele = treeData[i];
                if (ele.checked) {
                    scope._chooseItems.push(ele);
                }
                child = ele.children;
                if (child && child.length > 0) {
                    arguments.callee(child);
                }
            }
        };
        return function () {
            return new DataTreeHelper();
        };
    });
})();